<!DOCTYPE html>
<html>
<head>
<title>unsubscribe must resolve with true iff it is called on an active subscription</title>
<link rel="manifest" href="resources/push_manifest.json">
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/testdriver.js"></script>
<script src="../resources/testdriver-vendor.js"></script>
<script src="../serviceworker/resources/test-helpers.js"></script>
</head>
<body>
<script>
async_test(function(test) {
    var workerUrl = 'resources/empty_worker.js';
    var workerScope = 'resources/scope/' + location.pathname;
    var swRegistration;
    var pushSubscription;
    service_worker_unregister_and_register(test, workerUrl, workerScope)
        .then(function(serviceWorkerRegistration) {
            swRegistration = serviceWorkerRegistration;
            return wait_for_state(test, swRegistration.installing, 'activated');
        })
        .then(async function() {
            // If running manually, grant permission when prompted.
            if (window.test_driver) {
                await test_driver.set_permission(
                    {name: 'push', userVisibleOnly: true}, 'granted', false);
            }
            return swRegistration.pushManager.subscribe({ userVisibleOnly: true });
        })
        .then(function(subscription) {
            pushSubscription = subscription;
            assert_inherits(pushSubscription, 'unsubscribe',
                            'unsubscribe() should be exposed on the PushSubscription object');
            assert_equals(typeof(pushSubscription.unsubscribe), 'function',
                          'PushSubscription.unsubscribe() is a function.');
            return pushSubscription.unsubscribe();
        })
        .then(function(unsubscription_result) {
            assert_true(unsubscription_result,
                        "unsubscribe() called when correctly subscribed should be fulfilled with true");
            return pushSubscription.unsubscribe();
        })
        .then(function(unsubscription_result) {
            assert_false(unsubscription_result,
                         "unsubcribe() called a second time should be fulfilled with false");
            return swRegistration.pushManager.getSubscription();
        })
        .then(function(pushSubscription) {
            assert_equals(pushSubscription, null,
                          "After unsubscription, there is no more PushSubscription.");
            return service_worker_unregister_and_done(test, workerScope);
        })
        .catch(unreached_rejection(test));
}, 'unsubscribe must resolve with true iff it is called on an active subscription');
</script>
</body>
</html>
